﻿using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ANEPWebService.SerializableObjects
{
    public class XMLParseManager
    {   
        public static string ObjectToXML(SerializableCentro  pObject)
        {
            string result = string.Empty;
            XmlWriterSettings settings = new XmlWriterSettings();
            XmlWriter writer = null;
            XmlSerializer serializer = new XmlSerializer(typeof(SerializableCentro));
            XmlSerializerNamespaces nsp = new XmlSerializerNamespaces();
            MemoryStream memoryStream = new MemoryStream();
            StreamReader reader = null;

            try
            {
                if (pObject != null)
                {
                    settings.OmitXmlDeclaration = true;
                    settings.Indent = false;
                    settings.NewLineOnAttributes = false;

                    writer = XmlWriter.Create(memoryStream, settings);

                    nsp.Add(string.Empty, string.Empty);

                    serializer.Serialize(writer, pObject, nsp);
                    memoryStream.Position = 0L;
                    reader = new StreamReader(memoryStream);
                    result = reader.ReadToEnd();
                }
            }
            catch (Exception)
            {
                return null;
            }
            return result;
        }

        public static SerializableCentro XMLToStation(string pXML)
        {
            SerializableCentro result = null;
            SerializableCentro station = null;
            //List<StationStream> streams = null;
            //StationStream stream = null;

            XmlDocument document = new XmlDocument();
            XmlNodeList streamList = null;

            try
            {
                if (!pXML.Equals(string.Empty))
                {
                    //document.LoadXml(pXML);
                    //stationNode = document.SelectSingleNode("//Station");
                    //if (stationNode != null)
                    //{
                    //    station = new Station();
                    //    station.ID = stationNode.ChildNodes[0].InnerText;
                    //    station.Name = stationNode.ChildNodes[1].InnerText;
                    //    station.Status = stationNode.ChildNodes[2].InnerText;

                    //    streamList = stationNode.SelectNodes("StationStreamList/StationStream");
                    //    if (streamList != null && streamList.Count > 0)
                    //    {
                    //        streams = new List<StationStream>();
                    //        foreach (IXmlNode streamNode in streamList)
                    //        {
                    //            stream = new StationStream();
                    //            stream.ID = streamNode.ChildNodes[0].InnerText;
                    //            stream.URL = streamNode.ChildNodes[1].InnerText;
                    //            stream.BitRate = streamNode.ChildNodes[2].InnerText;
                    //            switch (streamNode.ChildNodes[3].InnerText)
                    //            {
                    //                case _MP3:
                    //                    stream.Type = StationStream.StreamType.MP3;
                    //                    break;
                    //                case _AAC:
                    //                    stream.Type = StationStream.StreamType.AAC;
                    //                    break;
                    //            }
                    //            streams.Add(stream);
                    //        }
                    //    }
                    //    station.Streams = streams;
                    //}
                }
            }
            catch (Exception)
            {
                return null;
            }
            result = station;
            return result;
        }

    }
}
